<template>
  <view class="main">
    <view class="header">
      <view class="search">
        <view class="search-box">
          <image class="search-icon" src="@/static/svg/icon_search.svg" />
          <input class="search-input" confirm-type="search" v-model="queryData.keyword" @confirm="productReload()" placeholder="搜索商品名称/编码/条码" />
        </view>
      </view>
      <view class="filter">
        <view class="filter-box">
          <view class="filter-type-item filter-classify" :class="{'active': groupTagCur}" @click="handleOpenFilter(dialogEunm.TAG_GROUPS)">
            {{ groupTagCur?.tagName || '全部分类' }}
          </view>
          <view class="filter-type-item filter-classify" :class="{'active': groupTagChildCur}" v-show="groupTagCur" @click="handleOpenFilter(dialogEunm.TAG_ITEMS)">
            {{groupTagChildTitle}} 
          </view>
          <view class="filter-type-item filter-sort" :class="{'active': queryData.sortType}" @click="handleOpenFilter(dialogEunm.SORT)">
            {{getSortTitle}}
          </view>
        </view>
        <view class="filter-dialog" :class="{'active': showFilterDialog}">
          <view class="tag-groups" v-if="dialogType === dialogEunm.TAG_GROUPS">
            <view 
              class="filter-item" 
              v-for="(item, index) in groupTags" 
              :key="index" 
              :class="{'active': groupTagCur?.id === item.id}"
              @click="handleTagGroup(item, index)"
            >{{item.tagName}}</view>
          </view>
          <view class="tag-groups" v-if=" dialogType === dialogEunm.TAG_ITEMS">
            <view class="filter-item" 
              v-for="(item, index) in groupTagChilds" 
              :key="index" 
              :class="{'active': groupTagChildCur?.id === item.id}"
              @click="handleTagItem(item, index)"
            >{{item.tagName}}</view>
          </view>
          <view class="tag-groups" v-if=" dialogType === dialogEunm.SORT">
            <view 
              class="filter-item" 
              v-for="(item, index) in sortTypes" 
              :key="index"  
              :class="{'active': queryData.sortType === item.value}"
              @click="handleSortItem(item, index)"
            >{{item.title}}</view>
          </view>
        </view>
      </view>
    </view>
    <view class="mask" v-if="showFilterDialog" @click="closeFilterDialog"></view>
    <view class="products">
      <view class="tit">产品列表</view>
      <view class="products-content">
        <template v-for="(item, index) in productList" :key="index">
          <Product :product="item" />
        </template>
      </view>
    </view>

  </view>
</template>

<script setup>
import { reactive, ref, computed } from 'vue'
import { onLoad, onReachBottom, onPullDownRefresh } from '@dcloudio/uni-app'
import Notices from '../../components/notices/index.vue' ;
import Product from '@/components/product/Product.vue' ;
import nva_new_product from '@/static/svg/nva_new_product.svg'
import nva_order from '@/static/svg/nva_order.svg'
import nva_hot from '@/static/svg/nva_hot.svg'
import nva_point from '@/static/svg/nva_point.svg'

import { getProductList, getTagListAllApi } from '@/api/product'
import { getTagGroup } from '@/utils/product'
// import 
const groupTags = ref([])
const dialogEunm = ref({
  'TAG_GROUPS': 'TAG_GROUPS',
  'TAG_ITEMS': 'TAG_ITEMS',
  'SORT': 'SORT',
})
const dialogType = ref('')

const sortTypes = ref([
  {
    title: '综合排序',
    value: 1,
  },
  {
    title: '价格从高到低',
    value: 2,
  },{
    title: '价格从高到低',
    value: 3,
  },
])

const getSortTitle = computed(()=> {
  if(queryData.sortType) {
    const sortType = sortTypes.value.find((item)=> item.value === queryData.sortType)
    return sortType?.title || '筛选'
  }
  return '筛选'
})
const queryData = reactive({
  page: 1,
  size: 10,
  productType: 1,
  sortType: '',
  tagIds: [],
  keyword: '',
})

const queryStatus = reactive({
  isLoaded: false,
  loading: false
})



const productList = ref([])

const showFilterDialog = ref(false)

const groupTagCur = ref(null)

const groupTagChilds = computed(()=> {
  const groups = groupTagCur.value
  if(groups?.subGroups) {
    return groups?.subGroups
  } 
  return []
})
const groupTagChildCur = ref(null)

const groupTagChildTitle = computed(()=> {
  // console.log('groupTagChildCur', groupTagChildCur)
  return groupTagChildCur.value?.tagName || `全部${groupTagCur.value?.tagName || ''}`
})

onLoad(()=> {
  init()
})

onPullDownRefresh( async ()=> {
  await init().finally(() => {
    uni.stopPullDownRefresh();
  });
})
onReachBottom(()=> {
  if(queryStatus.isLoaded){
    return
  }
  queryData.page += 1;
  getProduct()
})
async function init() {
  await productReload()
  getTagListAll()
}
async function productReload(){
  queryData.page = 1;
  queryStatus.isLoaded = false
  queryData.tagIds = []
  if(groupTagCur.value?.id) {
    queryData.tagIds.push(groupTagCur.value?.id)
  }
  if(groupTagChildCur.value?.id) {
    queryData.tagIds.push(groupTagChildCur.value?.id)
  }
  await getProduct()
}
async function getTagListAll(){
  // debugger
  const res = await getTagListAllApi({type: 1})
  groupTags.value =  getTagGroup(res)
  // console.log('groupTags', groupTags)
}

async function getProduct(){
  if(queryData.isLoaded) {
    return 
  }
  queryStatus.loading = true
  const res = await getProductList({
    ...queryData,
    tagIds: queryData.tagIds.toString()
  }).finally(()=> {
    queryStatus.loading = false
  })
  if(queryData.page === 1) {
    productList.value = [...res.items] 
  } else {
    productList.value.push(...res.items)
  }
  
  if(productList.value.length === res.total) {
    queryStatus.isLoaded = true
  } else {
    queryStatus.isLoaded = false
  }
}

function handleOpenFilter(type){
  dialogType.value = type
  showFilterDialog.value = true
}

function closeFilterDialog() {
  showFilterDialog.value = false
}

function handleTagGroup(data, index){

  // if(groupTagIndex.value !== index) {
  //   groupTagIndex.value = index 
  //   groupTagChildIndex.value = null 
  // } else {
  //   groupTagIndex.value = null 
  //   groupTagChildIndex.value = null 
  // }
  if(groupTagCur.value?.id === data.id) {
      groupTagCur.value = null
  } else {
      groupTagCur.value = data
  }
  groupTagChildCur.value = null
  closeFilterDialog()
  productReload()
}

function handleTagItem(data, index){
  if(groupTagChildCur.value?.id === data.id) {
      groupTagChildCur.value = null
  } else {
      groupTagChildCur.value = data
  }
  closeFilterDialog()
  productReload()
}

function handleSortItem(data, index) {
  queryData.sortType = data.value
  closeFilterDialog()
  productReload()
}

</script>

<style lang='scss'>
.main{
  min-height: 100vh;
  background: #F4F6F8;
  .header{
    position: relative;
    z-index: 5;
  }
  .search{
    position: relative;
    z-index: 10;
    height: 112rpx;
    background: #FFFFFF;
    border: 1rpx solid #EEEEEE;
    padding: 18rpx 32rpx;
    .search-box{
      height: 100%;
      background: #F2F2F2;
      border-radius: 55rpx;
      position: relative;
      .search-icon{
        position: absolute;
        z-index: 1;
        top: 50%;
        transform: translateY(-50%);
        width: 28rpx;
        height: 28rpx;
        left: 32rpx;
      }
      .search-input{
        display: flex;
        height: 100%;
        padding-left: 76rpx;
        align-items: center;
        font-weight: 400;
        font-size: 26rpx;
        color: #999999;
        line-height: 34rpx;
      }
    }

  }
  .filter{
    position: relative;
    .filter-box{
      height: 80rpx;
      background: #fff;
      display: flex;
      align-items: center;
      position: relative;
      z-index: 10;
      .filter-type-item{
        font-weight: 400;
        font-size: 26rpx;
        &.active{
          color: #FF8F1F;
        }
      }
      .filter-classify{
        flex: 1;
        line-height: 36rpx;
        padding-left: 35rpx;
      }
      .filter-sort{
        width: 156rpx;

      }
    }
    .filter-dialog{
      position: absolute;
      top: 100%;
      left: 0;
      width: 100%;
      background: #fff;
      z-index: -1;
      transform: translateY(-110%);
      transition: all .3s;
      &.active{
        transform: translateY(0);
      }
      .tag-groups{
        max-height: 50vh;
        overflow: auto;
        .filter-item{
          display: flex;
          align-items: center;
          padding: 0 32rpx;
          height: 98rpx;
          font-weight: 400;
          font-size: 26rpx;
          color: #333;
          line-height: 36rpx;
          &.active{
            color: #FF8F1F;
          }
        }
      }
    }
  }
  .mask{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
    background: rgba(0,0,0, 0.4);
  }
  .products{
    background: #fff;
    padding: 32rpx;
    margin-top: 20rpx;
    .tit{
      font-weight: 500;
      font-size: 28rpx;
      color: #333333;
      line-height: 36rpx;
      margin-bottom: 20rpx;
    }
    .products-content{
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 24rpx;
    }
  }
}
</style>
